package user

import (
	"context"
	"strconv"

	"mall/service/backend/api/internal/logic/backend/util"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserListLogic) UserList(req *types.UserListRequest) (resp *types.UserListResponse, err error) {
	logx.Infof("UserListRequest : %+v ", req)

	var page int64 = 1
	var pageSize int64
	var status int32
	if req.Current > 1 {
		page = req.Current
	}
	if req.PageSize > 0 { // 指定，检查最大值
		pageSize = req.PageSize
		if pageSize > l.svcCtx.Config.Query.MaxAmount {
			pageSize = l.svcCtx.Config.Query.MaxAmount
		}
	} else if req.PageSize == 0 { // 不指定，使用默认值
		pageSize = l.svcCtx.Config.Query.DefaultAmount
	} // 使用-1，rpc对应为0
	if req.Status != "" { // 不使用此字段为""，rpc对应为0
		if i, err := strconv.ParseInt(req.Status, 10, 32); err == nil {
			status = int32(i + 1)
		}
	}
	orderBys := util.ParseSorter(req.Sorter)
	filterBys := util.ParseFilter(req.Filter)
	res, err := l.svcCtx.BackendRpc.BackendGetUserList(l.ctx, &backendclient.UserListRequest{
		Current:  page,
		PageSize: pageSize,
		UserIds:  req.UserIds,
		Username: req.Username,
		Nickname: req.Nickname,
		Name:     req.Name,
		Email:    req.Email,
		Mobile:   req.Mobile,
		Status:   status,
		DeptId:   req.DeptId,
		RoleId:   req.RoleId,
		OrderBy:  orderBys,
		FilterBy: filterBys,
	})
	if err != nil {
		return nil, err
	}

	var list []*types.UserItem
	if len(res.List) > 0 {
		for _, item := range res.List {
			option := util.GetUserOption(item.Status, item.CurrentAuthority == "superAdmin")
			list = append(list, &types.UserItem{
				Id:               strconv.FormatInt(item.Id, 10),
				Username:         item.Username,
				Nickname:         item.Nickname,
				Name:             item.Name,
				Avatar:           item.Avatar,
				Title:            item.Title,
				Group:            item.Group,
				DeptId:           item.DeptId,
				Email:            item.Email,
				Mobile:           item.Mobile,
				Status:           item.Status,
				StatusText:       util.GetStatusText(item.Status),
				DelStatus:        item.DelStatus,
				CurrentAuthority: item.CurrentAuthority,
				// CreatedBy
				CreatedAt:        item.CreatedAt,
				// UpdatedBy
				UpdatedAt:        item.UpdatedAt,
				RoleId:           item.RoleId,
				RoleName:         item.RoleName,
				CanDisable:       option.CanDisable,
				CanEnable:        option.CanEnable,
				CanForceDelete:   option.CanForceDelete,
				CanRemove:        option.CanRemove,
				CanRestore:       option.CanRestore,
				CanResetPassword: option.CanResetPassword,
			})
		}
	}

	return &types.UserListResponse{
		Current:  page,
		PageSize: pageSize,
		List:     list,
		Total:    res.Total,
	}, nil
}
